import request from '@/utils/request'
import {blobValidate, tansParams} from "@/utils/ruoyi";
import errorCode from "@/utils/errorCode";
import {Message} from "element-ui";
export function download(url,params,filename) {
  return request.post(url, params, {
    transformRequest: [(params) => { return tansParams(params) }],
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    responseType: 'blob'
  }).then(async (data) => {
    const isBlob = blobValidate(data);
    if (isBlob) {
      const blob = new Blob([data])
      saveAs(blob, filename)
    } else {
      const resText = await data.text();
      const rspObj = JSON.parse(resText);
      const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
      Message.error(errMsg);
    }
  }).catch((r) => {
    console.error(r)
    Message.error('下载文件出现错误，请联系管理员！')
  })
}
export function queryExportProgress() {
  return request({
    url: '/certificate/certificate/exportAll/progress',
    method: 'get'
  })
}
// 导出合格证管理
export function exprotAllCertificate() {
  return request({
    url: '/certificate/certificate/exportAll',
    method: 'get'
  })
}
// 查询合格证打印数据列表
export function listCertificate(query) {
  return request({
    url: '/certificate/certificate/list',
    method: 'get',
    params: query
  })
}

// 查询合格证打印数据详细
export function getCertificate(id) {
  return request({
    url: '/certificate/certificate/' + id,
    method: 'get'
  })
}

// 新增合格证打印数据
export function addCertificate(data) {
  return request({
    url: '/certificate/certificate',
    method: 'post',
    data: data
  })
}

// 修改合格证打印数据
export function updateCertificate(data) {
  return request({
    url: '/certificate/certificate',
    method: 'put',
    data: data
  })
}

// 删除合格证打印数据
export function delCertificate(id) {
  return request({
    url: '/certificate/certificate/' + id,
    method: 'delete'
  })
}
